package P3;

public class StringExtractor {
    public static String extractStrings(String sourceCode) {
        StringBuilder extractedStrings = new StringBuilder();
        boolean insideString = false;
        boolean escapeMode = false;

        for (char c : sourceCode.toCharArray()) {
            if (insideString) {
                if (escapeMode) {
                    // 处理转义字符
                    switch (c) {
                        case 'n':
                            extractedStrings.append('\n');
                            break;
                        case 't':
                            extractedStrings.append('\t');
                            break;
                        case '\\':
                            extractedStrings.append('\\');
                            break;
                        case '"':
                            extractedStrings.append('"');
                            break;
                        default:
                            extractedStrings.append('\\').append(c);
                            break;
                    }
                    escapeMode = false;
                } else {
                    if (c == '\\') {
                        escapeMode = true;
                    } else if (c == '"') {
                        insideString = false;
                    } else {
                        extractedStrings.append(c);
                    }
                }
            } else if (c == '"') {
                insideString = true;
            }
        }

        return extractedStrings.toString();
    }

    public static void main(String[] args) {
        String sourceCode = "一般学习一门编程语言的时候，基本上第一个程序是输出\\\"Hello World!\\\"";
        String extractedStrings = extractStrings(sourceCode);
        System.out.println(extractedStrings);
    }
}
